package cn.school.mybatis.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import cn.school.mybatis.Dao.StudentDao;
import cn.school.mybatis.join.Student;
import cn.school.mybatis.xml.MyBatisUtil;

public class StudentDaoTest {

	public static void main(String[] args) {
		// search();
		// find();
		// update();
		search();
	}

	// 查找信息
	// 作业五5:为上次的查询功能加上分页:
	// (实现按照 按照 微信号,qq号,姓名,模糊查找学生的功能)
	public static void search() {
		SqlSession session = MyBatisUtil.get();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		List<Student> list = studentDao.search("牛", "14", "14", 5, 10);
		for (Student a : list) {
			System.out.println(a);
		}
		MyBatisUtil.close(session);
	}

	// 查找人数
	public static void find() {

		SqlSession session = MyBatisUtil.get();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		int count = studentDao.find("牛", "14", "14");
		System.out.println("一共有" + count + "人");
		MyBatisUtil.close(session);
	}

	// 添加人数
	public static void add() {
		SqlSession session = MyBatisUtil.get();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Student s = null;
		try {
			s = new Student("黑猪", "男", "159789456", "1147510270", "wechat12", 1, sdf.parse("2016-10-22"), "合肥",
					sdf.parse("1997-10-22"), "包河区", "zzq11475");
		} catch (ParseException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		int count = studentDao.add(s);
		System.out.println("s=" + count);
		MyBatisUtil.close(session);
	}

	// 删除学生
	public static void del() {
		SqlSession session = MyBatisUtil.get();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		int count = studentDao.del(201);
		System.out.println(count);
		MyBatisUtil.close(session);
	}

	public static void update() {
		SqlSession session = MyBatisUtil.get();
		StudentDao studentDao = session.getMapper(StudentDao.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		int number = 0;
		try {
			Student s = new Student(1, "heizhu", "男", "15978654963", "114856987", "wechat", 5, sdf.parse("2016-10-08"),
					"山东", sdf.parse("1997-10-28"), "淝南家园", null);
			number = studentDao.update(s);
		} catch (ParseException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			MyBatisUtil.close(session);
		}
		System.out.println(number);
	}
}
